

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="——若发现文章内容有误，敬请指正，望不吝赐教，感谢!@[TOC]  以往记录  Web项目实战 | 购物系统v2.0 | 开发记录（一）需求分析 | 技术选型 | 系统设计 | 数据表设计 | SpringBoot、SSM、Thymeleaf、Bootstrap…   Web项目实战 | 购物系统v2.0 | 开发记录（二）搭建SpringBoot+SSM框架环境 | 配置Druid+MyBat">
<meta property="og:type" content="article">
<meta property="og:title" content="Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索">
<meta property="og:url" content="http://example.com/2022/02/17/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv2.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E4%B9%9D%EF%BC%89Controller%E5%B1%82%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE%E7%9A%84%E5%B0%81%E8%A3%85%20_%20%E5%95%86%E5%93%81%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C%20_%20%E4%BA%94%E8%A1%A8%E8%81%94%E7%AB%8B%E5%AE%9E%E7%8E%B0%E5%95%86%E5%93%81%E6%90%9C%E7%B4%A2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="——若发现文章内容有误，敬请指正，望不吝赐教，感谢!@[TOC]  以往记录  Web项目实战 | 购物系统v2.0 | 开发记录（一）需求分析 | 技术选型 | 系统设计 | 数据表设计 | SpringBoot、SSM、Thymeleaf、Bootstrap…   Web项目实战 | 购物系统v2.0 | 开发记录（二）搭建SpringBoot+SSM框架环境 | 配置Druid+MyBat">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/97d7d0e6ee004b14b7a5ed8426be2897.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3772105845034b0ba8fb394c66bc9148.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6e975656781c41a2a08e40ab83a2b8bf.png">
<meta property="article:published_time" content="2022-02-17T14:42:30.000Z">
<meta property="article:modified_time" content="2022-08-22T15:50:45.830Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="JavaWeb">
<meta property="article:tag" content="项目实战:购物系统v2.0">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/97d7d0e6ee004b14b7a5ed8426be2897.png">
  
  
  
  <title>Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-02-17 22:42" pubdate>
          2022年2月17日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          15k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          125 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <blockquote>
<p>——若发现文章内容有误，敬请指正，望不吝赐教，感谢!<br>@[TOC]</p>
</blockquote>
<h1 id="以往记录"><a href="#以往记录" class="headerlink" title="以往记录"></a>以往记录</h1><hr>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122722369">Web项目实战 | 购物系统v2.0 | 开发记录（一）需求分析 | 技术选型 | 系统设计 | 数据表设计 | SpringBoot、SSM、Thymeleaf、Bootstrap…</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122726356">Web项目实战 | 购物系统v2.0 | 开发记录（二）搭建SpringBoot+SSM框架环境 | 配置Druid+MyBatis | 基于Bootstrap实现登陆页面| 图片验证码接口</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122743352">Web项目实战 | 购物系统v2.0 | 开发记录（三）分页显示 | 根据商品名称进行模糊查询</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122773805">Web项目实战 | 购物系统v2.0 | 开发记录（四）单个页面单个请求解决根据商品类型进行分页查询 | 使用省市区三级联动 | 使用JQuery 插件实现图片上传</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122787974">Web项目实战 | 购物系统v2.0 | 开发记录（五）| 使用base64编码实现头像修改 | 用户个人信息修改 | JQuery动态提示</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122799137">Web项目实战 | 购物系统v2.0 | 开发记录（六）商品详情页面 | 游客访问主页 | 运用Bootstrap4轻量级弹窗实现提示</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://editor.csdn.net/md/?articleId=122849396">Web项目实战 | 购物系统v2.0 | 开发记录（七）SpringBoot整合Shiro框架进行身份认证 | Shiro 加盐(MD5+Salt)验证登陆 | 数据表结构优化避免外键+设置中间表</a></p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/122970912">Web项目实战 | 购物系统v2.0 | 开发记录（八）前后端分离初步思路 | JS处理URL参数实用函数 | AJAX 向后台传递Map类型数据 | MyBatis多表查询优化</a></p>
</blockquote>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<p>windows10<br>IDEA 2021.3 专业版<br>JDK8<br>SpringBoot2.6.x<br>JQuery 3.x<br>Druid 1.2.5<br>Bootstrap 4.6.0<br>MySQL 8<br>Navicat 15</p>
<h1 id="一、设计Bean用于Controller层返回数据"><a href="#一、设计Bean用于Controller层返回数据" class="headerlink" title="一、设计Bean用于Controller层返回数据"></a>一、设计Bean用于Controller层返回数据</h1><hr>
<p>在之前的学习中用到了伪前后端分离的思路，前端通过像后端AJAX请求获取数据，然后再渲染数据到页面，在数据之间传递时通常需要对数据类型进行序列化，为了方便Controller层返回数据，这里使用泛型进行设计，设计实体类RestResponse，用于返回结果，同时设置一个支持返回JSON字符串的方法，toJSONString()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.base;<br><br><span class="hljs-keyword">import</span> com.alibaba.fastjson.JSON;<br><br><span class="hljs-comment">/*</span><br><span class="hljs-comment"> * @Description: 自定义响应数据结构</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RestResponse</span>&lt;T&gt; &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> code;<br>    <span class="hljs-keyword">private</span> String message;<br>    <span class="hljs-keyword">private</span> T response;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">RestResponse</span><span class="hljs-params">(<span class="hljs-type">int</span> code, String message)</span> &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-built_in">this</span>.message = message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">RestResponse</span><span class="hljs-params">(<span class="hljs-type">int</span> code, String message, T response)</span> &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-built_in">this</span>.message = message;<br>        <span class="hljs-built_in">this</span>.response = response;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> RestResponse <span class="hljs-title function_">fail</span><span class="hljs-params">(Integer code, String msg)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestResponse</span>&lt;&gt;(code, msg);<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> RestResponse <span class="hljs-title function_">fail</span><span class="hljs-params">(String msg)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestResponse</span>&lt;&gt;(-<span class="hljs-number">1</span>, msg);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> RestResponse <span class="hljs-title function_">ok</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">SystemCodeEnum</span> <span class="hljs-variable">systemCodeEnum</span> <span class="hljs-operator">=</span> SystemCodeEnum.OK;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestResponse</span>&lt;&gt;(systemCodeEnum.getCode(), systemCodeEnum.getMessage());<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> RestResponse <span class="hljs-title function_">ok</span><span class="hljs-params">(String message)</span> &#123;<br>        <span class="hljs-type">SystemCodeEnum</span> <span class="hljs-variable">systemCodeEnum</span> <span class="hljs-operator">=</span> SystemCodeEnum.OK;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestResponse</span>&lt;&gt;(systemCodeEnum.getCode(), message);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> &lt;F&gt;      the type parameter</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response the response</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> the rest response</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;F&gt; RestResponse&lt;F&gt; <span class="hljs-title function_">ok</span><span class="hljs-params">(F response)</span> &#123;<br>        <span class="hljs-type">SystemCodeEnum</span> <span class="hljs-variable">systemCodeEnum</span> <span class="hljs-operator">=</span> SystemCodeEnum.OK;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RestResponse</span>&lt;&gt;(systemCodeEnum.getCode(), systemCodeEnum.getMessage(), response);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setCode</span><span class="hljs-params">(<span class="hljs-type">int</span> code)</span> &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMessage</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> message;<br>    &#125;<br><br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setMessage</span><span class="hljs-params">(String message)</span> &#123;<br>        <span class="hljs-built_in">this</span>.message = message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> T <span class="hljs-title function_">getResponse</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> response;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setResponse</span><span class="hljs-params">(T response)</span> &#123;<br>        <span class="hljs-built_in">this</span>.response = response;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;RestResponse&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;code=&quot;</span> + code +<br>                <span class="hljs-string">&quot;, message=&#x27;&quot;</span> + message + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, response=&quot;</span> + response +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toJSONString</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> JSON.toJSONString(<span class="hljs-built_in">this</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在返回消息时，有许多是比较关键的信息，比如返回的状态、返回的信息等，所以可以设计一个枚举类，来表示返回的状态，以及对应的信息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.base;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> <span class="hljs-title class_">SystemCodeEnum</span> &#123;<br> <br>    OK(<span class="hljs-number">1</span>, <span class="hljs-string">&quot;成功&quot;</span>),<br>    AccessTokenError(<span class="hljs-number">400</span>, <span class="hljs-string">&quot;用户登录令牌失效&quot;</span>),<br>    UNAUTHORIZED(<span class="hljs-number">401</span>, <span class="hljs-string">&quot;用户未登录&quot;</span>),<br>    AuthError(<span class="hljs-number">402</span>, <span class="hljs-string">&quot;用户名或密码错误&quot;</span>),<br>    InnerError(<span class="hljs-number">500</span>, <span class="hljs-string">&quot;系统内部错误&quot;</span>),<br>    ParameterValidError(<span class="hljs-number">501</span>, <span class="hljs-string">&quot;参数验证错误&quot;</span>),<br>    AccessDenied(<span class="hljs-number">502</span>, <span class="hljs-string">&quot;用户没有权限访问&quot;</span>);<br><br>    <span class="hljs-type">int</span> code;<br>    String message;<br><br>    SystemCodeEnum(<span class="hljs-type">int</span> code, String message) &#123;<br>        <span class="hljs-built_in">this</span>.code = code;<br>        <span class="hljs-built_in">this</span>.message = message;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCode</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> code;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getMessage</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> message;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="二、商品批量操作"><a href="#二、商品批量操作" class="headerlink" title="二、商品批量操作"></a>二、商品批量操作</h1><hr>
<p>在进行后台管理时，批量操作是最常见的操作了，比如批量删除、批量修改等，这里笔者对于商品的管理添加了批量上下架的功能，从而修改一个商品的状态（目前只有上架和下架两种）。</p>
<p>在原来的数据库基础上添加一张表（还有一张中间表）用于表示商品的状态，表的结构如下：<br><img src="https://img-blog.csdnimg.cn/97d7d0e6ee004b14b7a5ed8426be2897.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>运行效果：<br><img src="https://img-blog.csdnimg.cn/3772105845034b0ba8fb394c66bc9148.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>在这个业务需求上有以下几个重要的地方。</p>
<h2 id="2-1-批量操作的前端设计"><a href="#2-1-批量操作的前端设计" class="headerlink" title="2.1 批量操作的前端设计"></a>2.1 批量操作的前端设计</h2><p><strong>思路：</strong> 在onclick属性指定一个JS函数，然后在JS里通过AJAX向后台发送更改商品状态的请求</p>
<p><strong>关键HTML代码：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 第一个部分，用于表示批量操作的按钮 --&gt;</span> <br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;text-align: right;&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;takeProducts(0)&quot;</span>&gt;</span>批量上架<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-warning&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;takeProducts(1)&quot;</span>&gt;</span>批量下架<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span>&gt;</span>批量删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 第二个部分，用于表示选中商品的选择框 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">...</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">thead</span> <span class="hljs-attr">...</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;cb_all&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span>&gt;</span>全选<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>            ...<br>         <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>         	...<br><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>每个商品的选择框则是在JS里通过AJAX返回的数据动态渲染的，一种简单的前后端分离思想</p>
<p><strong>关键JS代码：</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 当点击全选按钮后</span><br>$(<span class="hljs-string">&#x27;#cb_all&#x27;</span>).<span class="hljs-title function_">click</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)&#123;<br>    $(<span class="hljs-string">&#x27;.cb_product&#x27;</span>).<span class="hljs-title function_">prop</span>(<span class="hljs-string">&#x27;checked&#x27;</span>, $(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">prop</span>(<span class="hljs-string">&#x27;checked&#x27;</span>))<br>&#125;)<br><span class="hljs-comment">// 渲染商品列表信息(通过data属性标记商品ID)</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">renderProductInfo</span>(<span class="hljs-params">productInfo</span>)&#123;<br><br>    $(<span class="hljs-string">&#x27;#tbody-product&#x27;</span>).<span class="hljs-title function_">html</span>(<span class="hljs-string">``</span>)<br><br>    $.<span class="hljs-title function_">each</span>(productInfo, <span class="hljs-function">(<span class="hljs-params">index, prod</span>) =&gt;</span>&#123;<br>        $(<span class="hljs-string">&#x27;#tbody-product&#x27;</span>).<span class="hljs-title function_">append</span>(<span class="hljs-string">`</span><br><span class="hljs-string">          &lt;tr valign=&quot;baseline&quot;&gt;</span><br><span class="hljs-string">                &lt;td&gt;&lt;input type=&quot;checkbox&quot; class=&quot;cb_product&quot; data-id=&quot;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;id&#x27;</span>]&#125;</span>&quot;&gt;&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;id&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;provider&#x27;</span>][<span class="hljs-string">&#x27;name&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;name&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;prodtype&#x27;</span>][<span class="hljs-string">&#x27;name&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td class=&quot;text-danger&quot;&gt;￥<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;price&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;stock&#x27;</span>]&#125;</span>&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td class=&quot;text-center&quot;&gt;&lt;img src=&quot;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;img&#x27;</span>]&#125;</span>&quot; alt=&quot;加载中...&quot; style=&quot;width: 100px; height: 100px;&quot;/&gt;&lt;/td&gt;</span><br><span class="hljs-string">                &lt;td&gt;</span><br><span class="hljs-string">                     &lt;button class=&quot;btn btn-primary&quot;&gt;编辑&lt;/button&gt;</span><br><span class="hljs-string">                     &lt;button class=&quot;btn <span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;status&#x27;</span>] == <span class="hljs-number">1</span> ? <span class="hljs-string">&#x27;btn-warning&#x27;</span> : <span class="hljs-string">&#x27;btn-success&#x27;</span>&#125;</span>&quot;</span><br><span class="hljs-string">                            onclick=&quot;takeProduct(&#x27;<span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;id&#x27;</span>]&#125;</span>&#x27;, <span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;status&#x27;</span>]&#125;</span>)&quot;</span><br><span class="hljs-string">                     &gt;</span><br><span class="hljs-string">                     <span class="hljs-subst">$&#123;prod[<span class="hljs-string">&#x27;status&#x27;</span>] == <span class="hljs-number">1</span> ?<span class="hljs-string">&#x27;下架&#x27;</span>:<span class="hljs-string">&#x27;上架&#x27;</span>&#125;</span>&lt;/button&gt;</span><br><span class="hljs-string">                     &lt;button class=&quot;btn btn-danger&quot;&gt;删除&lt;/button&gt;</span><br><span class="hljs-string">                &lt;/td&gt;</span><br><span class="hljs-string">          &lt;/tr&gt;`</span>)<br>    &#125;)<br>&#125;<br><br><span class="hljs-comment">// 实现商品的上架 / 下架 </span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">takeProducts</span>(<span class="hljs-params">status</span>)&#123;<br>    <span class="hljs-keyword">let</span> reqData = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>()<br>    <span class="hljs-keyword">let</span> choseProduct = []<br>    <span class="hljs-comment">// 获取每个商品选择框的状态</span><br>    $(<span class="hljs-string">&#x27;.cb_product&#x27;</span>).<span class="hljs-title function_">each</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-keyword">if</span>($(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">prop</span>(<span class="hljs-string">&#x27;checked&#x27;</span>) === <span class="hljs-literal">true</span>)<br>            choseProduct.<span class="hljs-property">push</span> ($(<span class="hljs-variable language_">this</span>).<span class="hljs-title function_">data</span>(<span class="hljs-string">&#x27;id&#x27;</span>))<br>    &#125;)<br>    reqData[<span class="hljs-string">&#x27;type&#x27;</span>] = status == <span class="hljs-number">1</span> ? <span class="hljs-string">&#x27;downs&#x27;</span> : <span class="hljs-string">&#x27;ups&#x27;</span><br>    reqData[<span class="hljs-string">&#x27;prodId&#x27;</span>] = choseProduct<br>    $.<span class="hljs-title function_">ajax</span>(&#123;<br>        <span class="hljs-attr">url</span>: <span class="hljs-string">&#x27;/admin/product/update&#x27;</span>,<br>        <span class="hljs-attr">data</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(reqData),<br>        <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;POST&#x27;</span>,<br>        <span class="hljs-attr">dataType</span>: <span class="hljs-string">&#x27;JSON&#x27;</span>,<br>        <span class="hljs-attr">contentType</span>: <span class="hljs-string">&#x27;application/json;charset=utf-8&#x27;</span>,<br>        <span class="hljs-attr">error</span>: <span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> &#123;<br>            <span class="hljs-title function_">open_toast</span>(<span class="hljs-string">&#x27;fail&#x27;</span>, <span class="hljs-string">&#x27;修改商品失败!&#x27;</span>)<br>            <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">responseText</span>)<br>        &#125;,<br>        <span class="hljs-attr">success</span>: <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> &#123;<br>            <span class="hljs-title function_">open_toast</span>(data[<span class="hljs-string">&#x27;code&#x27;</span>] == <span class="hljs-number">1</span> ? <span class="hljs-string">&#x27;success&#x27;</span> : <span class="hljs-string">&#x27;warning&#x27;</span>,<br>                data[<span class="hljs-string">&#x27;message&#x27;</span>])<br>        &#125;<br>    &#125;)<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-2-批量操作的后端设计"><a href="#2-2-批量操作的后端设计" class="headerlink" title="2.2 批量操作的后端设计"></a>2.2 批量操作的后端设计</h2><p>前端结合HTML+JQuery实现了向后台发送AJAX请求，后端则需要在Controller层对数据进行处理，基于上一次的实践，后端可直接将其封装成Java原有的Map、List或Array等对象，接下来则采用Map的方式接收请求的数据。</p>
<p><strong>Controller层：</strong>（还有一种是单个商品上下架操作，思路差不多，都在这同一个请求中）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller.admin;<br><br><span class="hljs-keyword">import</span> com.github.pagehelper.PageHelper;<br><span class="hljs-keyword">import</span> com.github.pagehelper.PageInfo;<br><span class="hljs-keyword">import</span> com.uni.base.RestResponse;<br><span class="hljs-keyword">import</span> com.uni.pojo.Prodtype;<br><span class="hljs-keyword">import</span> com.uni.pojo.Product;<br><span class="hljs-keyword">import</span> com.uni.service.ProductService;<br><span class="hljs-keyword">import</span> com.uni.utils.PageUtil;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.PostMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestBody;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><br><span class="hljs-keyword">import</span> java.util.HashMap;<br><span class="hljs-keyword">import</span> java.util.List;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/admin/product&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ProductAdminController</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ProductService productService;<br>    <span class="hljs-meta">@PostMapping(&quot;/update&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">updateProduct</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody</span> Map&lt;String, Object&gt; map)</span>&#123;<br>        <span class="hljs-keyword">if</span>(map == <span class="hljs-literal">null</span>)<br>            <span class="hljs-keyword">return</span> RestResponse.fail(<span class="hljs-string">&quot;接收到的参数为空&quot;</span>).toJSONString();<br>        <span class="hljs-type">String</span> <span class="hljs-variable">type</span> <span class="hljs-operator">=</span> (String) map.getOrDefault(<span class="hljs-string">&quot;type&quot;</span>, <span class="hljs-string">&quot;&quot;</span>);<br>        <span class="hljs-comment">// 标记结果</span><br>        <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>        <span class="hljs-comment">// 批量操作</span><br>        <span class="hljs-keyword">if</span>(<span class="hljs-string">&quot;downs&quot;</span>.equals(type) || <span class="hljs-string">&quot;ups&quot;</span>.equals(type))&#123;<br>            List&lt;Long&gt; prodId = (List&lt;Long&gt;) map.getOrDefault(<span class="hljs-string">&quot;prodId&quot;</span>, <span class="hljs-literal">null</span>);<br>            <span class="hljs-keyword">if</span> (type.equals(<span class="hljs-string">&quot;downs&quot;</span>)) &#123;<br>                i = productService.takeThemDown(prodId);<br>            &#125;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(type.equals(<span class="hljs-string">&quot;ups&quot;</span>))&#123;<br>                i = productService.takeThemUp(prodId);<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-string">&quot;down&quot;</span>.equals(type) || <span class="hljs-string">&quot;up&quot;</span>.equals(type))&#123;<br>            <span class="hljs-type">Long</span> <span class="hljs-variable">prodId</span> <span class="hljs-operator">=</span> Long.parseLong((String) map.getOrDefault(<span class="hljs-string">&quot;prodId&quot;</span>, <span class="hljs-literal">null</span>));<br>            <span class="hljs-keyword">if</span> (type.equals(<span class="hljs-string">&quot;down&quot;</span>)) &#123;<br>                i = productService.takeItDown(prodId);<br>            &#125;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(type.equals(<span class="hljs-string">&quot;up&quot;</span>))&#123;<br>                i = productService.takeItUp(prodId);<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> i &gt; <span class="hljs-number">0</span> ? RestResponse.ok(<span class="hljs-string">&quot;操作成功!&quot;</span>).toJSONString() : RestResponse.fail(<span class="hljs-string">&quot;未修改到任何商品&quot;</span>).toJSONString();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>Service层是直接调用DAO层的相关方法，就不做记录了</p>
<p><strong>DAO层：</strong><br><code>ProductMapper.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">int</span> <span class="hljs-title function_">updateToDown</span><span class="hljs-params">(Long prodId)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">updateToUp</span><span class="hljs-params">(Long prodId)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">updateToDowns</span><span class="hljs-params">(List&lt;Long&gt; prodId)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">updateToUps</span><span class="hljs-params">(List&lt;Long&gt; prodId)</span>;<br></code></pre></td></tr></table></figure>
<p><code>ProductMapper.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 下架商品 --&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;updateToDown&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;Long&quot;</span>&gt;</span><br>        UPDATE v2_product_status SET status = 0<br>            WHERE id = #&#123;id&#125;<br>  <span class="hljs-tag">&lt;/<span class="hljs-name">update</span>&gt;</span><br><br>  <span class="hljs-tag">&lt;<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;updateToDowns&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;list&quot;</span>&gt;</span><br>    UPDATE v2_product_status SET status = 0<br>    WHERE id IN<br>        <span class="hljs-tag">&lt;<span class="hljs-name">foreach</span> <span class="hljs-attr">collection</span>=<span class="hljs-string">&quot;list&quot;</span> <span class="hljs-attr">separator</span>=<span class="hljs-string">&quot;,&quot;</span> <span class="hljs-attr">item</span>=<span class="hljs-string">&quot;pid&quot;</span> <span class="hljs-attr">open</span>=<span class="hljs-string">&quot;(&quot;</span> <span class="hljs-attr">close</span>=<span class="hljs-string">&quot;)&quot;</span>&gt;</span><br>          #&#123;pid&#125;<br>        <span class="hljs-tag">&lt;/<span class="hljs-name">foreach</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">update</span>&gt;</span><br><br><span class="hljs-comment">&lt;!--  上架商品--&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;updateToUp&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;Long&quot;</span>&gt;</span><br>    UPDATE v2_product_status SET status = 1<br>    WHERE id = #&#123;id&#125;<br>  <span class="hljs-tag">&lt;/<span class="hljs-name">update</span>&gt;</span><br><br>  <span class="hljs-tag">&lt;<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;updateToUps&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;list&quot;</span>&gt;</span><br>    UPDATE v2_product_status SET status = 1<br>    WHERE id IN<br>    <span class="hljs-tag">&lt;<span class="hljs-name">foreach</span> <span class="hljs-attr">collection</span>=<span class="hljs-string">&quot;list&quot;</span> <span class="hljs-attr">separator</span>=<span class="hljs-string">&quot;,&quot;</span> <span class="hljs-attr">item</span>=<span class="hljs-string">&quot;pid&quot;</span> <span class="hljs-attr">open</span>=<span class="hljs-string">&quot;(&quot;</span> <span class="hljs-attr">close</span>=<span class="hljs-string">&quot;)&quot;</span>&gt;</span><br>      #&#123;pid&#125;<br>    <span class="hljs-tag">&lt;/<span class="hljs-name">foreach</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">update</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="三、五表联立实现搜索"><a href="#三、五表联立实现搜索" class="headerlink" title="三、五表联立实现搜索"></a>三、五表联立实现搜索</h1><hr>
<p>同样是商品的操作，由于之前把商品结构表优化了一下，将含有外键的部分都分离开来，单独做一张表，同时添加一张中间表用作映射，一开始是比较麻烦，但是现在感觉还挺方便的，比如之前的需求就是表示商品的上下架，那么就需要商品具有状态这一属性，无需修改原来的商品表，而是添加商品-商品状态中间表，以及一张商品状态表即可。</p>
<p>所以将这些带有外键属性的表分离开来还是很有必要的。（MySQL学的不太好，不知道是哪个范式）<br><img src="https://img-blog.csdnimg.cn/6e975656781c41a2a08e40ab83a2b8bf.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>思路：</strong> 后端，直接修改MyBatis映射文件里的SQL语句，方便又快速。前端的话根据之前的思路，每次访问页面都会通过AJAX获取URL里的参数向后台请求数据，那么在这一个请求里就可以实现这种功能了。由此，前端的一些按钮，比如搜索、类型选择、下一页等，本质上都是直接先修改URL里的相关参数，然后跳转到修改后的URL。</p>
<p><strong>Controller层：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.controller.admin;<br><br><span class="hljs-keyword">import</span> com.github.pagehelper.PageHelper;<br><span class="hljs-keyword">import</span> com.github.pagehelper.PageInfo;<br><span class="hljs-keyword">import</span> com.uni.base.RestResponse;<br><span class="hljs-keyword">import</span> com.uni.pojo.Prodtype;<br><span class="hljs-keyword">import</span> com.uni.pojo.Product;<br><span class="hljs-keyword">import</span> com.uni.service.ProductService;<br><span class="hljs-keyword">import</span> com.uni.utils.PageUtil;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.PostMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestBody;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><br><span class="hljs-keyword">import</span> java.util.HashMap;<br><span class="hljs-keyword">import</span> java.util.List;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/admin/product&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ProductAdminController</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    ProductService productService;<br>    <span class="hljs-meta">@PostMapping(&quot;/query&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">queryProduct</span><span class="hljs-params">(<span class="hljs-meta">@RequestBody(required = false)</span> Map&lt;String, String&gt; map)</span>&#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">pageNow</span> <span class="hljs-operator">=</span> map == <span class="hljs-literal">null</span> ? <span class="hljs-number">1</span> : Integer.parseInt(map.getOrDefault(<span class="hljs-string">&quot;pageNow&quot;</span>, <span class="hljs-string">&quot;1&quot;</span>));<br>        PageHelper.startPage(pageNow, <span class="hljs-number">6</span>);<br>        List&lt;Product&gt; products = productService.getProductWithProviderBySome(map);<br>        <span class="hljs-type">PageInfo</span> <span class="hljs-variable">pageInfo</span> <span class="hljs-operator">=</span> PageUtil.setPageInfo(pageNow, <span class="hljs-number">6</span>, products);<br>        List&lt;Prodtype&gt; prodtypes = productService.getAllProdType();<br>        <span class="hljs-keyword">return</span> RestResponse.ok(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;String, Object&gt;() &#123;&#123;<br>            put(<span class="hljs-string">&quot;pageInfo&quot;</span>, pageInfo);<br>            put(<span class="hljs-string">&quot;prodtypes&quot;</span>, prodtypes);<br>        &#125;&#125;).toJSONString();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>可以看到，在Controller层只获取了当前页面的参数，关于搜索的参数则交给DAO层处理</p>
<p>Service层是直接调用的DAO层接口，这里就不做记录了</p>
<p><strong>DAO层：</strong><br>首先说明一下POJO类，在Product商品类里定义了其他两个自定义的POJO类，分别是商品类型Prodtype、还有供应商Provider</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.pojo;<br><br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Product</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br>    <span class="hljs-keyword">private</span> Long id;<br><br>    <span class="hljs-keyword">private</span> String name;<br><br>    <span class="hljs-keyword">private</span> Long stock;<br><br>    <span class="hljs-keyword">private</span> Float price;<br><br>    <span class="hljs-keyword">private</span> String detail;<br><br>    <span class="hljs-keyword">private</span> String img;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">long</span> <span class="hljs-variable">serialVersionUID</span> <span class="hljs-operator">=</span> <span class="hljs-number">1L</span>;<br>    <span class="hljs-comment">// 分割线</span><br>    <span class="hljs-keyword">private</span> Provider provider;<br>    <span class="hljs-keyword">private</span> Prodtype prodtype;<br>    <span class="hljs-keyword">private</span> Integer status;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>ProductMapper.java</code> 这里使用了@Param注解表示指定变量名，这里指定为map，方便Mapper映射文件识别</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">List&lt;Product&gt; <span class="hljs-title function_">selectProductWithProviderBySome</span><span class="hljs-params">(<span class="hljs-meta">@Param(&quot;map&quot;)</span> Map&lt;String, String&gt; paramMap)</span>;<br></code></pre></td></tr></table></figure>
<p><code>ProductMapper.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;selectProductWithProviderBySome&quot;</span> <span class="hljs-attr">parameterType</span>=<span class="hljs-string">&quot;Map&quot;</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">&quot;productWithProvider&quot;</span>&gt;</span><br>    SELECT product.*, prodtype.name as ptname, prov.name as provName, ps.status<br>    FROM v2_product as product<br>    	LEFT JOIN v2_product_type as pt<br>    		ON product.id = pt.prodId<br>    	LEFT JOIN v2_prodtype prodtype<br>    		ON pt.typeId = prodtype.id<br>    	LEFT JOIN v2_product_provider as pp<br>    		ON product.id = pp.prodId<br>    	LEFT JOIN v2_provider as prov<br>    		ON prov.id = pp.provId<br>    	LEFT JOIN v2_product_status as ps<br>    		ON ps.prodId = product.id<br>    <span class="hljs-tag">&lt;<span class="hljs-name">where</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;map != null&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">foreach</span> <span class="hljs-attr">collection</span>=<span class="hljs-string">&quot;map&quot;</span> <span class="hljs-attr">index</span>=<span class="hljs-string">&quot;key&quot;</span> <span class="hljs-attr">item</span>=<span class="hljs-string">&quot;value&quot;</span> <span class="hljs-attr">separator</span>=<span class="hljs-string">&quot;AND&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;key == &#x27;prodtype&#x27;&quot;</span>&gt;</span><br>            prodtype.name LIKE CONCAT(&#x27;%&#x27;, #&#123;value&#125;, &#x27;%&#x27;)<br>          <span class="hljs-tag">&lt;/<span class="hljs-name">if</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;key == &#x27;productName&#x27;&quot;</span>&gt;</span><br>            product.name LIKE CONCAT(&#x27;%&#x27;,#&#123;value&#125;,&#x27;%&#x27;)<br>          <span class="hljs-tag">&lt;/<span class="hljs-name">if</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">&quot;key == &#x27;prodName&#x27;&quot;</span>&gt;</span><br>              product.name LIKE CONCAT(&#x27;%&#x27;, #&#123;value&#125;, &#x27;%&#x27;)<br>            <span class="hljs-tag">&lt;/<span class="hljs-name">if</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">foreach</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">if</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">where</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br>  <br> <span class="hljs-tag">&lt;<span class="hljs-name">resultMap</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;productWithProvider&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;com.uni.pojo.Product&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;id&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;id&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;name&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;name&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;stock&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;stock&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;price&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;price&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;img&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;img&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;detail&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;detail&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;status&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;status&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">association</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;provider&quot;</span> <span class="hljs-attr">javaType</span>=<span class="hljs-string">&quot;com.uni.pojo.Provider&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;provName&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;name&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">association</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">association</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;prodtype&quot;</span> <span class="hljs-attr">javaType</span>=<span class="hljs-string">&quot;com.uni.pojo.Prodtype&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">result</span> <span class="hljs-attr">column</span>=<span class="hljs-string">&quot;ptname&quot;</span> <span class="hljs-attr">property</span>=<span class="hljs-string">&quot;name&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">association</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">resultMap</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>通过观察可以发现，在Mapper里直接遍历了Map，并通过<code>&lt;if&gt;</code>标签判断设置一些条件判断，这就使得笔者要添加新的查询条件就十分容易，前端增加一些组件按钮或者选择框之类的，点击后在URL里添加参数比如&amp;status&#x3D;1指定状态为1，后端只需在Mapper映射文件里的map循环遍历部分添加一个<code>&lt;if test=&quot;status != null&quot;&gt; status = #&#123;value&#125; &lt;/if&gt;</code>就能实现多个条件查询了。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv2-0/" class="category-chain-item">项目实战:购物系统v2.0</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaWeb/">#JavaWeb</a>
      
        <a href="/tags/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98-%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv2-0/">#项目实战:购物系统v2.0</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索</div>
      <div>http://example.com/2022/02/17/Web项目实战 _ 购物系统v2.0 _ 开发记录（九）Controller层返回数据的封装 _ 商品批量操作 _ 五表联立实现商品搜索/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年2月17日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/02/17/2022%E5%B9%B4%E6%9C%80%E6%96%B0%20%E9%AB%98%E6%B8%85%E5%AD%A6%E4%B9%A0%E8%B7%AF%E7%BA%BF%E5%9B%BE%20_%20Java%E3%80%81%E5%A4%A7%E6%95%B0%E6%8D%AE%E3%80%81UI_UE/" title="2022年最新 高清学习路线图 _ Java、大数据、UI_UE">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">2022年最新 高清学习路线图 _ Java、大数据、UI_UE</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/02/17/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv2.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E5%85%AB%EF%BC%89%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB%E5%88%9D%E6%AD%A5%E6%80%9D%E8%B7%AF%20_%20JS%E5%A4%84%E7%90%86URL%E5%8F%82%E6%95%B0%E5%AE%9E%E7%94%A8%E5%87%BD%E6%95%B0%20_%20AJAX%20%E5%90%91%E5%90%8E%E5%8F%B0%E4%BC%A0%E9%80%92Map%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%20_%20MyBatis%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E4%BC%98%E5%8C%96/" title="Web项目实战 _ 购物系统v2.0 _ 开发记录（八）前后端分离初步思路 _ JS处理URL参数实用函数 _ AJAX 向后台传递Map类型数据 _ MyBatis多表查询优化">
                        <span class="hidden-mobile">Web项目实战 _ 购物系统v2.0 _ 开发记录（八）前后端分离初步思路 _ JS处理URL参数实用函数 _ AJAX 向后台传递Map类型数据 _ MyBatis多表查询优化</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
